﻿
using LH.Model.PU;
using Microsoft.Extensions.Logging;
using Quartz;
using RestSharp;
using SqlSuarEntity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using XXX.api.Controllers;
using XXX.api.Log;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.XXXEntities;
using Yonyou.U8.BO;

namespace XXX.api.Quarzs
{
    public class UserInfoSyncjob2 : IJob
    {
        private readonly ILogger<UserInfoSyncjob2> _logger;
        //private readonly ILoggerHelper _logger;

        //  private readonly ICache _cache;
        public UserInfoSyncjob2(ILogger<UserInfoSyncjob2> logger)
        {
            //_cache = cache;
            _logger = logger;
        }
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
                        {
                            //.....
                            // Console.WriteLine($"{DateTime.Now.ToString()}：开始执行同步第三方数据");
                            _logger.LogInformation("开始执行定时推送MES同步;");
                            //只有浙江恒富的时候，进行定时任务推送
                            if (CanShu.cOrg=="NBHF")
                            {
                                //生产订单推送
                                string sql = string.Format(@"select distinct a.MoCode  from mom_order a,mom_orderdetail b where a.MoId = b.MoId  
and  isnull(define31,'') = '' and a.createdate >= '2025-04-01'");
                                List<string> lstMoCode = SqlsugarHelper.Instance.Ado.SqlQuery<string>(sql);
                                NBHFSDCVM nBHFSDCVM = new NBHFSDCVM(CanShu.conStr);
                                for(int i=0;i<lstMoCode.Count;i++)
                                {
                                    NBHF_XieTong nBHF_XieTong = new NBHF_XieTong();
                                    nBHF_XieTong.cOper = "add";
                                    nBHF_XieTong.cLx = "生产订单";
                                    nBHF_XieTong.cCode = lstMoCode[i];
                                    // 处理业务逻辑
                                    Models.RetModel2 ret = nBHFSDCVM.XieTong(nBHF_XieTong);
                                    // 将 JSON 数据保存到数据库中
                                    JsonSaveService.saveLog<NBHF_XieTong>(nBHF_XieTong, "XieTong", nBHF_XieTong.ToString(), ret.code, ret.message);
                                }

                                //材料出库单单推送
                                 sql = string.Format(@"select distinct  a.cCode   from rdrecord11 a ,rdrecords11 b  where a.id =b.id and 
 a.dDate>='2025-04-01'  and isnull(b.cmocode,'')<>''
 and  isnull(b.cdefine31,'')<>'推送成功'  and isnull(b.cdefine31,'') not like '%生产工单不存在%'  ");
                               lstMoCode = SqlsugarHelper.Instance.Ado.SqlQuery<string>(sql); 
                                for (int i = 0; i < lstMoCode.Count; i++)
                                {
                                    NBHF_XieTong nBHF_XieTong = new NBHF_XieTong();
                                    nBHF_XieTong.cOper = "add";
                                    nBHF_XieTong.cLx = "材料出库";
                                    nBHF_XieTong.cCode = lstMoCode[i];
                                    // 处理业务逻辑
                                    Models.RetModel2 ret = nBHFSDCVM.XieTong(nBHF_XieTong);
                                    // 将 JSON 数据保存到数据库中
                                    JsonSaveService.saveLog<NBHF_XieTong>(nBHF_XieTong, "XieTong", nBHF_XieTong.ToString(), ret.code, ret.message);
                                }

                                //到货单检验推送
                                sql = string.Format(@"select distinct a.cCode 
from PU_ArrivalVouch a
inner join PU_ArrivalVouchs b on a.id= b.id 
inner join inventory i on b.cinvcode= i.cinvcode
left join UFSystem..UA_User u on a.cMaker = u.cUser_Name
where   i.cinvdefine2='是' and isnull(cdefine10,'')=''  and a.ddate>='2025-04-01' ");
                               lstMoCode = SqlsugarHelper.Instance.Ado.SqlQuery<string>(sql); 
                                for (int i = 0; i < lstMoCode.Count; i++)
                                {
                                    NBHF_XieTong nBHF_XieTong = new NBHF_XieTong();
                                    nBHF_XieTong.cOper = "add";
                                    nBHF_XieTong.cLx = "到货报检";
                                    nBHF_XieTong.cCode = lstMoCode[i];
                                    // 处理业务逻辑
                                    Models.RetModel2 ret = nBHFSDCVM.XieTong(nBHF_XieTong);
                                    // 将 JSON 数据保存到数据库中
                                    JsonSaveService.saveLog<NBHF_XieTong>(nBHF_XieTong, "XieTong", nBHF_XieTong.ToString(), ret.code, ret.message);
                                }

                                //调拨申请单检验推送， 现在没数据，改成调拨单推 
                                sql = string.Format(@"select distinct a.cTVCode  
from TransVouch a
inner join TransVouchs b  on a.ID= b.id  
inner join inventory i on b.cinvcode= i.cinvcode
left join UFSystem..UA_User u on a.cMaker = u.cUser_Name
where a.dTVDate>='2025-04-01' and a.cowhcode='01'  AND ISNULL(cdefine10,'')='' ");
                                lstMoCode = SqlsugarHelper.Instance.Ado.SqlQuery<string>(sql);
                                for (int i = 0; i < lstMoCode.Count; i++)
                                {
                                    NBHF_XieTong nBHF_XieTong = new NBHF_XieTong();
                                    nBHF_XieTong.cOper = "add";
                                    nBHF_XieTong.cLx = "调拨报检";
                                    nBHF_XieTong.cCode = lstMoCode[i];
                                    // 处理业务逻辑
                                    Models.RetModel2 ret = nBHFSDCVM.XieTong(nBHF_XieTong);
                                    // 将 JSON 数据保存到数据库中
                                    JsonSaveService.saveLog<NBHF_XieTong>(nBHF_XieTong, "XieTong", nBHF_XieTong.ToString(), ret.code, ret.message);
                                }
                                //发货单检验推送，改成销售出库单推送
                                sql = string.Format(@"select distinct a.cCode 
 from rdrecord32 a
inner join rdrecords32 b  on a.ID= b.ID
inner join inventory i on b.cinvcode= i.cinvcode
left join UFSystem..UA_User u on a.cMaker = u.cUser_Name
where  isnull(cdefine10,'')='' 
and a.dDate>='2025-04-01' ");
                                lstMoCode = SqlsugarHelper.Instance.Ado.SqlQuery<string>(sql);
                                for (int i = 0; i < lstMoCode.Count; i++)
                                {
                                    NBHF_XieTong nBHF_XieTong = new NBHF_XieTong();
                                    nBHF_XieTong.cOper = "add";
                                    nBHF_XieTong.cLx = "发货报检";
                                    nBHF_XieTong.cCode = lstMoCode[i];
                                    // 处理业务逻辑
                                    Models.RetModel2 ret = nBHFSDCVM.XieTong(nBHF_XieTong);
                                    // 将 JSON 数据保存到数据库中
                                    JsonSaveService.saveLog<NBHF_XieTong>(nBHF_XieTong, "XieTong", nBHF_XieTong.ToString(), ret.code, ret.message);
                                }

                            }





                        });
        }
    }
}
